﻿package  wibrst.loading {
	/**
	 * 所有Loading的基类，定义了在Loading过程中需要处理的几种情况的纯虚函数。
	 * 
	 * */
	import flash.display.MovieClip;
	import flash.events.Event;
	
	public class VKernelLoading extends MovieClip {
		
		static public const LOAD_ALL_COMP:String = "LOAD_ALL_COMP";
		
		/// debug
		protected var bDetailTrace:Boolean;
		
		/// load management
		protected var nComp:Number;
		
		private var bLoading:Boolean;
		
		
		/// for inherit
		
		/**
		 * 纯虚函数，用以定义：在初始时应当执行的动作
		 */
		protected function initAction():void { }
		
		/**
		 * 纯虚函数，用以定义: 在确认开始载入时应当执行的动作
		 */
		protected function startAction():void { }
		
		/**
		 * 纯虚函数，用以定义: 在进度发生改变时应当执行的动作
		 */
		protected function progAction( nPercent:Number, sPercent:String ):void { }
		
		/**
		 * 纯虚函数，用以定义: 在ENTER_FRAME事件执行的动作
		 */
		protected function runAction():void { }
		
		/**
		 * 纯虚函数，用以定义: 在完成所有Load后执行的动作
		 */
		protected function compAction():void { }
		
		/**
		 * 供派生类启动载入调用
		 */
		protected function startLoadingBase( ):void {
			if ( !bLoading ) {
				if(bDetailTrace) trace( "[VKernelLoading] startLoading" );
				bLoading = true;
				nComp = 0;
				
				startAction();
			}else {
				if(bDetailTrace) trace( "[VKernelLoading] now already have a loading !!!");
			}
		}
		
		/**
		 * 供派生类完成载入时调用
		 */
		protected function compLoading():void{
			bLoading = false;
		}
		
		/// public (for inherit)
		
		/// base
		
		public function VKernelLoading() {
			if(bDetailTrace) trace( "[VKernelLoading] Construct!" );
			bDetailTrace = true;
			bLoading = false;
			
			addEventListener(Event.ENTER_FRAME, run);
			addEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
			
			initAction();
		}
		
		private function removedHandler(e:Event):void {
			removeEventListener(Event.REMOVED_FROM_STAGE, removedHandler);
			removeEventListener(Event.ENTER_FRAME, run);
		}
		
		private function run(e:Event):void {
			runAction();
		}
		
	}
	
	
	/**
	 * ..
	 * @author wibrst@qq.com
	 */
	
}